package cn.com.scitc.dao;

import cn.com.scitc.mapper.ManagerMapper;
import cn.com.scitc.model.Manager;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ManagerDao {
    public Manager findById(Integer id){
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()){
            ManagerMapper mapper = sqlSession.getMapper(ManagerMapper.class);
            return mapper.selectByPrimaryKey(id);
        }
    }

    public Manager findByLoginId(String loginId){
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()){
            ManagerMapper mapper = sqlSession.getMapper(ManagerMapper.class);
            return mapper.findByLoginId(loginId);
        }
    }

    public int updateByPrimaryKey(Manager manager){
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()){
            ManagerMapper mapper = sqlSession.getMapper(ManagerMapper.class);
            final int result = mapper.updateByPrimaryKey(manager);
            sqlSession.commit();
            return result;
        }
    }

    public List<Manager> selectAll(){
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()){
            ManagerMapper mapper = sqlSession.getMapper(ManagerMapper.class);
            return mapper.selectAll();
        }
    }

    public Integer insert(Manager manager){
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()){
            ManagerMapper mapper = sqlSession.getMapper(ManagerMapper.class);
            int result = mapper.insert(manager);
            sqlSession.commit();
            return result;
        }
    }

    public Integer delete(Integer id){
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()){
            ManagerMapper mapper = sqlSession.getMapper(ManagerMapper.class);
            int result = mapper.deleteByPrimaryKey(id);
            sqlSession.commit();
            return result;
        }
    }
}
